package com.example.fw;

import static com.example.fw.ContactHelper.CREATION;
import static com.example.fw.ContactHelper.MODIFICATION;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.AddressData;
import com.example.utils.ListOf;
import com.example.utils.SortedListOf;


public class ContactHelper extends BaseHelper{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<AddressData> cachedAddressFirstName; //
	
	
	public SortedListOf<AddressData> getAddressFirstName() { //
		if (cachedAddressFirstName == null){
			rebuildCacheFN();
		}
		return cachedAddressFirstName;
	}
	
	private void rebuildCacheFN() {
		cachedAddressFirstName = new SortedListOf<AddressData>(); //
		
		manager.navigatTo().mainPage();
		List <WebElement> columnFN = driver.findElements(By.xpath("//table[@id='maintable']//tr[@name='entry']//td[2]"));
		for (WebElement fn : columnFN) {
			String firstname = fn.getText();
			cachedAddressFirstName.add(new AddressData().withFirstname(firstname));
		}
	}

	public ContactHelper createAddress(AddressData addressdata) {
		manager.navigatTo().mainPage();
	    initAddressCreation();
	 	fillAddressForm(addressdata, CREATION);
	 	submitAddressCreation();
	 	gotoMainPage();
	 	rebuildCacheFN();
	 	return this;
	}
	
	public ContactHelper modifyAddress(int i, AddressData addressdata) {
		initAddressModification(i);
		fillAddressForm(addressdata, CREATION);
		submitAddressModification();
		gotoMainPage();
		rebuildCacheFN();
		return this;
	}
	
	public ContactHelper deleteAddress(int i) {
		initAddressModification(i);
		submitAddressDeletion();
		gotoMainPage();
		rebuildCacheFN();
		return this;
	}	

	
	//---------------------------------------------------------------------------------------------



	public ContactHelper initAddressCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillAddressForm(AddressData addressdata, boolean formType) {
		type(By.name("firstname"), addressdata.getFirstname());
		type(By.name("lastname"), addressdata.getLastname());
	    type(By.name("address"), addressdata.getAddress());
	    type(By.name("home"), addressdata.getHomephone());
	    type(By.name("mobile"), addressdata.getMobilephone());
	    type(By.name("work"), addressdata.getWorkphone());
	    type(By.name("email"), addressdata.getEmail());
	    type(By.name("email2"), addressdata.getEmail2());
	    selectByText(By.name("bday"), addressdata.getBday());
	    selectByText(By.name("bmonth"), addressdata.getBmonth());
	    type(By.name("byear"), addressdata.getByear());
	    if (formType == CREATION){
	    	//  selectByText(By.name("new_group"), "group 1");
	    } else {
	    	if(driver.findElements(By.name("new_group")).size() != 0){
	    		throw new Error ("Group selector exists in contact modification form");
	    	}
	    }
	    type(By.name("address2"), addressdata.getAddress2());
	    type(By.name("phone2"), addressdata.getHomephone2());
	    
		return this;
	}

	

	public ContactHelper submitAddressCreation() {
		click(By.name("submit"));
		cachedAddressFirstName = null;
		return this;
	}

	public ContactHelper gotoMainPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initAddressModification(int index) {
		int constanta=2;
		int line = constanta + index;
		click(By.xpath("//table[@id='maintable']//tr[" + line + "]//img[@title='Edit']"));
		return this;
	}

	public ContactHelper submitAddressModification() {
		click(By.name("update"));
		cachedAddressFirstName = null;
		return this;
	}

	public ContactHelper submitAddressDeletion() {
		click(By.xpath("//input[@name='update'][@value='Delete']"));
		cachedAddressFirstName = null;
		return this;
	}

	

}
